home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / wu_ftpd_37_21.lha / wu-ftpd / src / getusershell.c < prev    next >
C/C++ Source or Header  |  1994-08-08  |  3KB  |  144 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)getusershell.c    5.6 (Berkeley) 6/1/90";
  22.  
  23. #endif /* LIBC_SCCS and not lint */
  24.  
  25. #ifndef AMIGA
  26. #include "../src/config.h"
  27. #else
  28. #include "config.h"
  29. #endif
  30.  
  31. #include <sys/types.h>
  32. #include <sys/param.h>
  33. #include <sys/file.h>
  34. #include <sys/stat.h>
  35. #include <stdlib.h>
  36. #include <ctype.h>
  37. #include <stdio.h>
  38.  
  39. #ifdef AMIGA
  40. #define SHELLS "AmiTCP:db/shells"
  41. #else
  42. #define SHELLS "/etc/shells"
  43. #endif
  44.  
  45. /*
  46.  * Do not add local shells here.  They should be added in /etc/shells
  47.  */
  48. static char *okshells[] =
  49. #ifndef AMIGA
  50. {"/bin/sh", "/bin/csh", 0};
  51. #else
  52. {"shell", "cli", 0};
  53. #endif
  54.  
  55. static char **shells,
  56.  *strings;
  57. static char **curshell = NULL;
  58. static char **initshells();
  59.  
  60. /*
  61.  * Get a list of shells from SHELLS, if it exists.
  62.  */
  63. char *
  64. getusershell(void)
  65. {
  66.     char *ret;
  67.  
  68.     if (curshell == NULL)
  69.         curshell = initshells();
  70.     ret = *curshell;
  71.     if (ret != NULL)
  72.         curshell++;
  73.     return (ret);
  74. }
  75.  
  76. void
  77. endusershell(void)
  78. {
  79.     if (shells != NULL)
  80.         free((char *) shells);
  81.     shells = NULL;
  82.     if (strings != NULL)
  83.         free(strings);
  84.     strings = NULL;
  85.     curshell = NULL;
  86. }
  87.  
  88. void
  89. setusershell(void)
  90. {
  91.     curshell = initshells();
  92. }
  93.  
  94. static char **
  95. initshells(void)
  96. {
  97.     register char **sp,
  98.      *cp;
  99.     register FILE *fp;
  100.     struct stat statb;
  101.  
  102.     if (shells != NULL)
  103.         free((char *) shells);
  104.     shells = NULL;
  105.     if (strings != NULL)
  106.         free(strings);
  107.     strings = NULL;
  108.     if ((fp = fopen(SHELLS, "r")) == (FILE *) 0)
  109.         return (okshells);
  110.     if (fstat(fileno(fp), &statb) == -1) {
  111.         (void) fclose(fp);
  112.         return (okshells);
  113.     }
  114.     if ((strings = (char *) malloc((unsigned) statb.st_size + 1)) == NULL) {
  115.         (void) fclose(fp);
  116.         return (okshells);
  117.     }
  118.     shells = (char **) calloc((unsigned) statb.st_size / 3, sizeof(char *));
  119.  
  120.     if (shells == NULL) {
  121.         (void) fclose(fp);
  122.         free(strings);
  123.         strings = NULL;
  124.         return (okshells);
  125.     }
  126.     sp = shells;
  127.     cp = strings;
  128.     while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
  129. #ifndef AMIGA
  130.         while (*cp != '#' && *cp != '/' && *cp != '\0')
  131.             cp++;
  132. #endif
  133.         if (*cp == '#' || *cp == '\0')
  134.             continue;
  135.         *sp++ = cp;
  136.         while (!isspace(*cp) && *cp != '#' && *cp != '\0')
  137.             cp++;
  138.         *cp++ = '\0';
  139.     }
  140.     *sp = (char *) 0;
  141.     (void) fclose(fp);
  142.     return (shells);
  143. }
  144.